<?php
namespace App\Models\Main;

use App\Utilities\Utility;

class ApiAccessModel extends BaseModel
{
    /**
     * 接口调用权限
     */

    protected $table = 'api_access';
    protected $fillable = [
        'id','plat','access','created_at','createTime',
    ];

    /**
     * plat 调用平台：1APP端，2PC端
     * access 权限值
     */

    /**
     * 获取当天接口权限值
     * @param $plat 调用平台，1APP，2PC
     */
    public function getApiAccess($plat)
    {
        $dayStart = strtotime(date('Ymd',time()).'000001');
        $dayEnd = strtotime(date('Ymd',time()).'235959');
        $model = ApiAccessModel::where('plat',$plat)
            ->where('created_at','>',$dayStart)
            ->where('created_at','<',$dayEnd)
            ->first();
        if (!$model) {
            $access = Utility::getRandStr2(rand(60,80));
            $access = $access.date('YmdHis',time()).rand(10000,99999);
            ApiAccessModel::insertGetId([
                'plat' => $plat,
                'access' => $access,
                'created_at' => time(),
                'createTime' => date('Y-m-d H:i:s',time()),
            ]);
        } else {
            $access = $model->access;
        }
        return $access;
    }
}